<template>
  <BasicModal
    v-bind="$attrs"
    width="35%"
    :height="370"
    title="充值"
    @register="register"
    :loading="loading"
    loading-tip="加载中"
    @ok="submit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
import { BasicForm, useForm } from '@/components/Form';
import { rechargeFormSchema } from '@/views/admin/company/data';
const { createMessage } = useMessage();
// const propsData = ref();
const loading = ref(true);
const [register, { closeModal }] = useModalInner((data) => {
  // propsData.value = data.companyId;
  resetFields();
  getFormData(data.companyId);
});
const [
  registerForm,
  { resetFields, getForm, getFieldsValue, setFieldsValue, validate ,clearValidate},
] = useForm({
  labelWidth: 100,
  schemas: rechargeFormSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 24 },
  // mode: 'edit',
});
// 确定提交
async function submit() {
  const formData = await validate();
  const res = await defHttp.post({
    url: '/admin/company/recharge_submit',
    data: JSON.stringify(formData),
  });
  if (res) {
    closeModal();
    createMessage.success('充值成功');
  } else {
    createMessage.error('充值失败');
  }
}
async function getFormData(companyId) {
  const res = await defHttp.post({
    url: '/admin/company/recharge',
    data: { companyId: companyId },
  });
  if (res) {
    setFieldsValue({
      companyId: res.company.id,
      companyName: res.company.name,
      companyUserName: res.companyAdmin.username,
      companyBalance: res.company.balance,
      amount: '',
      payMethodId: '',
      memo: '',
      sysAdminName: res.sysAdmin.name,
    });
    loading.value = false;
  }
}
const handleResizeColumn = (w, col) => {
  col.width = w;
};
</script>
